//
//  NewPurchaseViewController.m
//  YYPHSpeedJump
//
//  Created by speedjump on 16/3/14.
//  Copyright © 2016年 sujiang. All rights reserved.
//

#import "NewPurchaseViewController.h"
#import "MyPurchaseCollectionViewCell.h"
#import "PurchaseDetailViewController.h"
@interface NewPurchaseViewController ()
{
    NSInteger page;
}

@end

@implementation NewPurchaseViewController
- (NSMutableArray *)newArray
{
    if (!_newArray) {
        _newArray = [NSMutableArray array];
        
    }
    return _newArray;
}
- (void)viewWillAppear:(BOOL)animated
{
    page=1;
    
    self.tabBarController.tabBar.hidden = YES;
    [self requestData];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"最新心愿";
    
    [self createView];
}
- (void)createView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing =9*kWidthScale;
    layout.itemSize = CGSizeMake((kScreenWidth-20*kWidthScale)/3, 145);
    NSLog(@"%f",[UIScreen mainScreen].bounds.size.width);
    layout.sectionInset = UIEdgeInsetsMake(5, 5*kWidthScale,5,5*kWidthScale);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-10) collectionViewLayout:layout];
    self.collectionView.backgroundColor =[UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource =self;
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"MyPurchaseCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"myPurchaseCell"];

}
- (void)requestData
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",kBaseUrl,kXYHomeUrl];
    NSDictionary *dict = @{@"page":[NSNumber numberWithInteger:page]};
    [NetworkHandle getUpDataWithUrl:urlStr class:NSClassFromString(@"PurchaseModel") dictionary:dict firstKey:@"data" secondKey:@"list" success:^(NSArray *array) {
        if (page == 1) {
            [self.newArray removeAllObjects];
        }

        [self.newArray addObjectsFromArray:array];
         self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        
        [self.collectionView.footer endRefreshing];
        [self.collectionView.header  endRefreshing];

        [self.collectionView reloadData];
        
        
    }];
    
}
#pragma mark-------------下拉刷新 上拉加载--------------------
- (void)headerRereshing
{
    page =1;
    [self requestData];
    
}
- (void)footerRefresh
{
    page++;
    [self requestData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.newArray.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyPurchaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myPurchaseCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = self.newArray[indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PurchaseDetailViewController *purchaseDetailVC =[[PurchaseDetailViewController alloc]init];
    [self.navigationController pushViewController:purchaseDetailVC animated:YES];
    purchaseDetailVC.purchaseModel =self.newArray[indexPath.row];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
